// miniprogram/pages/homePage/usedGoods/goodsList/goodsList.js
const db = wx.cloud.database()
const _ = db.command

Page({

    /**
     * 页面的初始数据
     */
    data: {
        message: {
            type: '',  //消息提示类型
            showMessage: false,
            text: '',   //消息提示内容,
            icon: ''   //图标
        },
        goodsList: [],     //货物列表
        seekValue: '',    //搜索内容
        leftName: "全部",
        rightName: "排序",
        showActionsheet: false,    //是否显示上拉列表
        groups: [],   //上拉列表
        show: '', //是否显示totop按钮
        skip: 0,//商品列表的开始位置
        totop: '',  //回顶
    },
    footSheet(e) {  //上拉菜单
        this.data.newButton = e.currentTarget.dataset.type
        if (e.currentTarget.dataset.type == "leftbutton") {
            var list = [
                { name: '全部' },
                { name: '生活百货' },
                { name: '服装鞋帽' },
                { name: '电器' },
                { name: '美妆' },
                { name: '数码设备' },
                { name: '运动户外' },
                { name: '零食水果' },
                { name: '其它' },
            ]
        } else {
            var list = [
                { name: "热门" },
                { name: '最新发布' },
                { name: '价格低' },
                { name: '价格高' },
            ]
        }
        this.setData({
            showActionsheet: true,
            groups: list
        })

    },
    getList(e) {  //货物列表
        wx.showLoading({
            mask: true,
            title: '加载中',
        })
        wx.cloud.callFunction({
            // 云函数名称
            name: 'getgoodsList',
            // 传给云函数的参数
            data: {
                sort: "usedGoods",
                skip: this.data.skip,
                category: this.data.leftName,
                type: this.data.rightName,
                seekValue: this.data.seekValue
            },
        }).then(res => {
            let newData = res.result.list || res.result.data
            if (newData.length != 0) { //有新数据
                newData.forEach(element => {
                    element.time = element.time.slice(0, 10)
                });
                if (this.data.skip) {  //触底
                    this.setData({
                        goodsList: this.data.goodsList.concat(newData)
                    })
                } else {
                    this.setData({  //新条件
                        goodsList: newData
                    })
                }

            } else {  //没有数据
                if (this.data.skip) { //触底
                    this.data.skip -= 10
                } else {//新条件
                    this.setData({
                        goodsList: [],

                    })
                }
                this.setData({
                    message: {
                        showMessage: true,
                        text: "已经没有更多了",
                        type: "warn",
                        icon: "warning"
                    }
                })
                setTimeout(() => {
                    this.setData({
                        message: {
                            showMessage: false
                        }
                    })
                }, 1500)
            }
            wx.hideLoading()
        }).catch(console.error)
    },
    shop(e) {//加入购物车
        wx.cloud.callFunction({
            // 云函数名称
            name: 'changeGoodsList',
            // 传给云函数的参数
            data: {
                type: "gwCar_list",
                goods: e.currentTarget.dataset.item
            },
        }).then(res => {
            // console.log(res.result) // 3
            if (res.result != "repeat") {
                this.setData({
                    message: {
                        showMessage: true,
                        text: "成功加入购物车",
                        type: "success",
                        icon: "checked"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        message: {
                            showMessage: false
                        }
                    })
                }, 1500)
            } else {
                this.setData({
                    message: {
                        showMessage: true,
                        text: "已经加入购物车了",
                        type: "warn",
                        icon: "warning"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        message: {
                            showMessage: false
                        }
                    })
                }, 1500)
            }
        }).catch(console.error)

    },
    coll(e) {  //收藏
        // console.log(e.currentTarget.dataset);

        wx.cloud.callFunction({
            // 云函数名称
            name: 'changeGoodsList',
            // 传给云函数的参数
            data: {
                type: "collect_list",
                id: e.currentTarget.dataset.id
            },
        }).then(res => {
            // console.log(res.result) // 3
            if (res.result != "repeat") {
                this.setData({
                    message: {
                        showMessage: true,
                        text: "成功收藏",
                        type: "success",
                        icon: "checked"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        message: {
                            showMessage: false
                        }
                    })
                }, 1500)
            } else {
                this.setData({
                    message: {
                        showMessage: true,
                        text: "已经收藏了",
                        type: "warn",
                        icon: "warning"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        message: {
                            showMessage: false
                        }
                    })
                }, 1500)
            }
        }).catch(console.error)
    },
    gotoDetails(e) {
        // console.log(e.currentTarget.dataset);

        wx.navigateTo({
            url: '/pages/homePage/usedGoods/details/details',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('to_details', {
                    mes: e.currentTarget.dataset.item,
                    status: "1"
                })
            }
        })
    },
    totop() {
        this.setData({
            totop: 0
        })
    },
    ss() {
        this.data.skip = 0
        this.getList()
    },
    // vant
    btnClick(e) {  //选择
        if (this.data.newButton == "leftbutton") {
            this.data.skip = 0
            this.setData({
                showActionsheet: false,
                leftName: e.detail.name
            })
        } else if (this.data.newButton == "rightbutton") {
            this.data.skip = 0
            this.setData({
                showActionsheet: false,
                rightName: e.detail.name
            })
        }
        this.getList()
    },
    onClose() {  //取消
        this.setData({ showActionsheet: false });
    },

    onChange(e) {   //搜索改变
        if (e.detail != "") {
            this.setData({
                seekValue: e.detail
            });
        }

    },
    scrolltolower() {
        wx.showLoading({
            mask: true,
            title: '加载中',
        })

        this.data.skip += 10
        this.getList()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})